<?php
 
//创建WebSocket Server对象，监听0.0.0.0:9502端口
$ws = new Swoole\WebSocket\Server('0.0.0.0', 9502);
 
//监听WebSocket连接打开事件
$ws->on('Open', function ($ws, $request) {
    echo "进入者为::{$request->fd}\n";
});
 
//监听WebSocket消息事件
$ws->on('Message', function ($ws, $frame) {
    echo "接受到消息::{$frame->data}\n";
    $ws->push($frame->fd,"你刚刚发了: {$frame->data}");
});
 
//监听WebSocket连接关闭事件
$ws->on('Close', function ($ws, $fd) {
    echo "用户id::{$fd}离开\n";
});
 
$ws->start();
 
?>